package com.qf.spring.anno.service.impl;

import com.qf.spring.anno.dao.UserMapper;
import com.qf.spring.anno.entity.User;
import com.qf.spring.anno.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true,rollbackFor = Exception.class)
    public User login(String name, String password) {
        List<User> users = userMapper.selectByName(name);
        for (User user : users) {
            if (Objects.nonNull(user)) {
                if (Objects.equals(user.getPassword(), password)) {
                    // 用户名和密码正确
                    return user;
                }
            }
        }
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true,rollbackFor = Exception.class)
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public int addUser(User user) {
        return userMapper.add(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.update(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.delete(id);
    }
}
